﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class NewsBll : INewsBll
    {
        private INewsDal _news;
        private IMapper _mapper;
        public NewsBll(INewsDal news, IMapper mapper)
        {
            _news = news;
            _mapper = mapper;
        }

        public async Task<int> AddNewsAsync(string title, string info, int browse, string sourceDescription)
        {
            return await _news.AddAsync(new News 
            {
                Title = title,
                Info = info,
                Browse = browse,
                SourceDescription = sourceDescription
            });
        }

        public async Task<int> DeleteNewsAsync(int id)
        {
            var data = await _news.QueryAsync(id);
            return data == null ? -1 : await _news.DeleteAsync(data);
        }

        public async Task<int> EditNewsAsync(int id, string title, string info, int browse, string sourceDescription)
        {
            var data = await _news.QueryAsync(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.Info = info;
            data.Browse = browse;
            data.SourceDescription = sourceDescription;
            return await _news.EditAsync(data);
        }

        public async Task<List<NewsDto>> GetNewsAllAsync()
        {
            var data = await _news.QueryAsync();
            return _mapper.Map<List<News>,List<NewsDto>>(data);
        }

        public async Task<NewsDto> QueryNewsByIdAsync(int id)
        {
            var data = await _news.QueryAsync(id);
            return _mapper.Map<News, NewsDto>(data);
        }

        public async Task<List<NewsDto>> QueryNewsByNameAsync(string title)
        {
            var data = await _news.QueryAsync(n => n.Title.Contains(title));
            return _mapper.Map<List<News>, List<NewsDto>>(data);
        }
    }
}
